﻿@model QueuedEmailListModel
@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    //page title
    ViewBag.Title = T("Admin.System.QueuedEmails").Text;
}
@using (Html.BeginForm())
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Administration/Content/images/ico-system.png")" alt="" />
            @T("Admin.System.QueuedEmails")
        </div>
        <div class="options">
            <input type="button" id="delete-selected" class="k-button" value="@T("Admin.System.QueuedEmails.DeleteSelected")" />
            <input type="submit" name="delete-all" class="k-button" value="@T("Admin.System.QueuedEmails.DeleteAll")" onclick="return confirm('@T("Admin.Common.AreYouSure")'); " />
        </div>
    </div>
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchStartDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchStartDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchEndDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchEndDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchFromEmail):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchFromEmail)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchToEmail):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchToEmail)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchLoadNotSent):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchLoadNotSent)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SearchMaxSentTries):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SearchMaxSentTries)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.GoDirectlyToNumber):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.GoDirectlyToNumber)
                <input type="submit" name="go-to-email-by-number" class="k-button" value="@T("Admin.Common.Go")" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="btnSearch" class="k-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                <div id="queuedEmails-grid"></div>

                <script>
                    $(document).ready(function () {
                        $("#queuedEmails-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("QueuedEmailList", "QueuedEmail"))",
                                        type: "POST",
                                        dataType: "json",
                                        data: additionalData
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors"
                                },
                                error: function(e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                pageSize: @(gridPageSize),
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true
                            },
                            scrollable: false,
                            dataBound: onDataBound,
                            columns: [{
                                field: "Id",
                                headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: "<input type='checkbox' value='#=Id#' class='checkboxGroups'/>",
                                width: 50
                            }, {
                                field: "Id",
                                title: "@T("Admin.System.QueuedEmails.Fields.Id")",
                                width: 50
                            }, {
                                field: "Subject",
                                title: "@T("Admin.System.QueuedEmails.Fields.Subject")",
                                width: 300
                            }, {
                                field: "From",
                                title: "@T("Admin.System.QueuedEmails.Fields.From")",
                                width: 100
                            }, {
                                field: "To",
                                title: "@T("Admin.System.QueuedEmails.Fields.To")",
                                width: 100
                            }, {
                                field: "CreatedOn",
                                title: "@T("Admin.System.QueuedEmails.Fields.CreatedOn")",
                                width: 200,
                                type: "date",
                                format: "{0:G}"
                            }, {
                                field: "SentOn",
                                title: "@T("Admin.System.QueuedEmails.Fields.SentOn")",
                                width: 200,
                                type: "date",
                                format: "{0:G}"
                            }, {
                                field: "Priority",
                                title: "@T("Admin.System.QueuedEmails.Fields.Priority")",
                                width: 100
                            }, {
                                field: "Id",
                                title: "@T("Admin.Common.Edit")",
                                width: 50,
                                template: '<a href="Edit/#=Id#">@T("Admin.Common.Edit")</a>'
                            }]
                        });
                    });
                </script>
                <script type="text/javascript">
                    var selectedIds = [];

                    $(document).ready(function () {

                        //search button
                        $('#btnSearch').click(function () {
                            //search
                            var grid = $('#queuedEmails-grid').data('kendoGrid');
                            grid.dataSource.page(1); //new search. Set page size to 1
                            grid.dataSource.read();
                            //clear selected checkboxes
                            $('.checkboxGroups').attr('checked', false).change();
                            selectedIds = [];
                            return false;
                        });

                        $("#@Html.FieldIdFor(model => model.SearchFromEmail)").keydown(function (event) {
                            if (event.keyCode == 13) {
                                $("#btnSearch").click();
                                return false;
                            }
                        });

                        $("#@Html.FieldIdFor(model => model.SearchToEmail)").keydown(function (event) {
                            if (event.keyCode == 13) {
                                $("#btnSearch").click();
                                return false;
                            }
                        });

                        //"delete selected" button
                        $('#delete-selected').click(function (e) {
                            e.preventDefault();

                            var postData = {
                                selectedIds: selectedIds
                            };

                            $.ajax({
                                cache:false,
                                type: "POST",
                                url: "@(Url.Action("DeleteSelected", "QueuedEmail"))",
                                data: postData,
                                complete: function (data) {
                                    //reload grid
                                    var grid = $('#queuedEmails-grid').data('kendoGrid');
                                    grid.dataSource.page(1); //new search. Set page size to 1
                                    grid.dataSource.read();
                                    //clear selected checkboxes
                                    $('.checkboxGroups').attr('checked', false).change();
                                    selectedIds = [];
                                },
                                error:function (xhr, ajaxOptions, thrownError){
                                    alert(thrownError);
                                },
                                traditional: true
                            });
                            return false;
                        });

                        $('#mastercheckbox').click(function () {
                            $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
                        });

                        //wire up checkboxes.
                        $('#queuedEmails-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                            var $check = $(this);
                            if ($check.is(":checked") == true) {
                                var checked = jQuery.inArray($check.val(), selectedIds);
                                if (checked == -1) {
                                    //add id to selectedIds.
                                    selectedIds.push($check.val());
                                }
                            }
                            else {
                                var checked = jQuery.inArray($check.val(), selectedIds);
                                if (checked > -1) {
                                    //remove id from selectedIds.
                                    selectedIds = $.grep(selectedIds, function (item, index) {
                                        return item != $check.val();
                                    });
                                }
                            }
                            updateMasterCheckbox();
                        });
                    });

                    function additionalData() {
                        return {
                            SearchStartDate: $('#@Html.FieldIdFor(model => model.SearchStartDate)').val(),
                            SearchEndDate: $('#@Html.FieldIdFor(model => model.SearchEndDate)').val(),
                            SearchFromEmail: $('#@Html.FieldIdFor(model => model.SearchFromEmail)').val(),
                            SearchToEmail: $('#@Html.FieldIdFor(model => model.SearchToEmail)').val(),
                            SearchLoadNotSent: $('#@Html.FieldIdFor(model => model.SearchLoadNotSent)').is(':checked'),
                            SearchMaxSentTries: $('#@Html.FieldIdFor(model => model.SearchMaxSentTries)').val()
                        };
                    }

                    function onDataBound(e) {

                        $('#queuedEmails-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                            var currentId = $(this).val();
                            var checked = jQuery.inArray(currentId, selectedIds);
                            //set checked based on if current checkbox's value is in selectedIds.
                            $(this).attr('checked', checked > -1);
                        });

                        updateMasterCheckbox();
                    }

                    function updateMasterCheckbox() {
                        var numChkBoxes = $('#queuedEmails-grid input[type=checkbox][id!=mastercheckbox]').length;
                        var numChkBoxesChecked = $('#queuedEmails-grid input[type=checkbox][checked][id!=mastercheckbox]').length;
                        $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
                    }
                </script>
            </td>
        </tr>
    </table>
}